package com.nf.practice.workdemo;

import lombok.Data;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        List<TmWaterDetails> tmWaterDetailsList = new ArrayList<>(); // 假设这里有你的TmWaterDetails对象列表
        // 手动创建并填充TmWaterDetails对象列表
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            tmWaterDetailsList.add(new TmWaterDetails(sdf.parse("2024-03-15"), new BigDecimal("100"), "CounterpartyA", "123", new BigDecimal("100")));
            tmWaterDetailsList.add(new TmWaterDetails(sdf.parse("2024-03-15"), new BigDecimal("100"), "CounterpartyB", "456", new BigDecimal("200")));
            tmWaterDetailsList.add(new TmWaterDetails(sdf.parse("2024-03-16"), new BigDecimal("300"), "CounterpartyA", "123", new BigDecimal("300")));
            tmWaterDetailsList.add(new TmWaterDetails(sdf.parse("2024-03-16"), new BigDecimal("300"), "CounterpartyC", "789", new BigDecimal("400")));
            tmWaterDetailsList.add(new TmWaterDetails(sdf.parse("2024-03-16"), new BigDecimal("300"), "CounterpartyC", "789", new BigDecimal("400")));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<TmWaterDetails> resultList = tmWaterDetailsList.stream()
                .collect(Collectors.groupingBy(
                        tm -> Arrays.asList(tm.getTradeDate(), tm.getPay())
                ))
                .values().stream()
                .flatMap(detailsList -> {
                    Set<String> counterpartySet = new HashSet<>();
                    BigDecimal amount = null;

                    for (TmWaterDetails details : detailsList) {
                        if (counterpartySet.contains(details.getCounterpartyName()) || counterpartySet.contains(details.getCounterpartyNo())) {
                            amount = BigDecimal.ZERO;
                            break;
                        }

                        counterpartySet.add(details.getCounterpartyName());
                        counterpartySet.add(details.getCounterpartyNo());
                        amount = details.getIncome();
                    }

                    final BigDecimal finalAmount = amount;
                    return detailsList.stream().map(details -> new TmWaterDetails(details.getTradeDate(), details.getPay(), details.getCounterpartyName(), details.getCounterpartyNo(), finalAmount));
                })
                .collect(Collectors.toList());

        System.out.println("Result List: " + resultList);
    }

    @Data
    static class TmWaterDetails {
        private Date tradeDate;
        private BigDecimal pay;
        private String counterpartyName;
        private String counterpartyNo;
        private BigDecimal income;

        public TmWaterDetails(Date tradeDate, BigDecimal pay, String counterpartyName, String counterpartyNo, BigDecimal income) {
            this.tradeDate = tradeDate;
            this.pay = pay;
            this.counterpartyName = counterpartyName;
            this.counterpartyNo = counterpartyNo;
            this.income = income;
        }

        // Getters and setters

        @Override
        public String toString() {
            return "TmWaterDetails{" +
                    "tradeDate=" + tradeDate +
                    ", pay=" + pay +
                    ", counterpartyName='" + counterpartyName + '\'' +
                    ", counterpartyNo='" + counterpartyNo + '\'' +
                    ", income=" + income +
                    '}';
        }
    }
}
